﻿Public Class DPRICEPACK

    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub DEFAULTPRICEPACKAGE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim objSelectMasproductWithPriceDate As New masPrice
        Dim maspriceDataByPackage As DataTable
        maspriceDataByPackage = connect.GetTable(objSelectMasproductWithPriceDate.selectPckproduct_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        pckproductGrid.PrimaryGrid.DataSource = maspriceDataByPackage

        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(a.`dfmid` AS CHAR(8)) AS DFMID, a.`formula` AS 'FORMULA', b.`fmname` AS 'FMNAME', CAST(a.`l_margin` AS CHAR(50)) AS 'L_MARGIN', CAST(a.`h_margin` AS CHAR(50)) AS 'H_MARGIN' FROM masdformula a JOIN masformula b ON a.`fmid` = b.`fmid` WHERE a.`status` = '1'"
        s = New FILTERCLASS(OPDPriceFormulaTxt, sql, "รหัสสูตร,สูตร,ชื่อสูตร,ต่ำสุด,สูงสุด", "50,100,100,50,50", "1,1,1,1,1", "1,1,1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        Dim s2 As FILTERCLASS
        Dim sql2 As String = "SELECT CAST(a.`dfmid` AS CHAR(8)) AS DFMID, a.`formula` AS 'FORMULA', b.`fmname` AS 'FMNAME', CAST(a.`l_margin` AS CHAR(50)) AS 'L_MARGIN', CAST(a.`h_margin` AS CHAR(50)) AS 'H_MARGIN' FROM masdformula a JOIN masformula b ON a.`fmid` = b.`fmid` WHERE a.`status` = '1'"
        s2 = New FILTERCLASS(IPDPriceFormulaTxt, sql2, "รหัสสูตร,สูตร,ชื่อสูตร,ต่ำสุด,สูงสุด", "50,100,100,50,50", "1,1,1,1,1", "1,1,1,1,1")
        's.SetShowInDown = False
        s2.SetShowBorder = True

        DesignForm(Me)
        DesignGrid(pckproductGrid)
        DesignGrid(pcklistproductGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub pckproductGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles pckproductGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        'PRDCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่มผลิตภัณฑ์").Value()
        'PRDNAMELabel.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อผลิตภัณฑ์").Value()

        selectmasPriceByPckproduct(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่มผลิตภัณฑ์").Value())
    End Sub

    Public Sub selectmasPriceByPckproduct(ByVal pckProductID As Integer)
        Dim queryString As String
        queryString = "SELECT a.`prdcode` AS 'รหัสผลิตภัณฑ์', b.`prdname` AS 'ชื่อผลิตภัณฑ์', b.`prdcat` AS 'รหัสกลุ่มผลิตภัณฑ์', c.`catname` AS 'กลุ่มผลิตภัณฑ์', d.`st` AS 'วันเริ่มต้น', d.`en` AS 'วันสิ้นสุด', b.`last_cost` AS 'ต้นทุนสุดท้าย', b.`avg_cost` AS 'ต้นทุนเฉลี่ย', d.`opd_price` AS 'OPD', d.`ipd_price` AS 'IPD', d.`prcid` AS 'รหัสราคา' FROM pcklistproduct a LEFT JOIN masproduct b ON a.`prdcode` = b.`prdcode` LEFT JOIN masprdcats c ON b.`prdcat` = c.`prdcat` LEFT JOIN (SELECT * FROM masprice WHERE NOW() BETWEEN st AND en) d ON a.`prdcode` = d.`prdcode` WHERE a.pckpid = '" & pckProductID & "'"

        Dim maspriceByPckproductData As DataTable
        maspriceByPckproductData = connect.GetTable(queryString)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        pcklistproductGrid.PrimaryGrid.DataSource = maspriceByPckproductData
    End Sub

    Private Sub pcklistproductGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles pcklistproductGrid.CellClick
        selectMaspriceHistoryBypckg(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value())
    End Sub

    Public Sub selectMaspriceHistoryBypckg(ByVal prdCode As Integer)
        Dim queryString As String
        queryString = "SELECT `prcid` AS 'รหัสราคา', `prdcode` AS 'รหัสผลิตภัณฑ์', `st` AS 'วันเริ่มต้น', `en` AS 'วันสิ้นสุด', `opd_price` AS 'OPD', `ipd_price` AS 'IPD', `status` AS 'สถานะการเปิดใช้งาน' FROM masprice WHERE prdcode = '" & prdCode & "' ORDER BY st DESC"

        Dim maspriceHistoryByPckgData As DataTable
        maspriceHistoryByPckgData = connect.GetTable(queryString)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        pcklistproductHistoryGrid.PrimaryGrid.DataSource = maspriceHistoryByPckgData

        Me.Refresh()

        For i As Integer = 0 To maspriceHistoryByPckgData.Rows.Count - 1
            Dim dateStart As Date
            Dim dateEnd As Date
            dateStart = maspriceHistoryByPckgData.Rows(i)("วันเริ่มต้น")
            dateEnd = maspriceHistoryByPckgData.Rows(i)("วันสิ้นสุด")

            Dim result1 As Integer = DateTime.Compare(dateStart, Now)
            Dim result2 As Integer = DateTime.Compare(dateEnd, Now)

            'Dim result1 As Integer = DateDiff(DateInterval.Day, dateStart, Date.Now)
            'Dim result2 As Integer = DateDiff(DateInterval.Day, dateEnd, Date.Now)

            'MsgBox(result1 & "/" & result2)

            If result1 < 0 And result2 < 0 Then
                'MsgBox("past")
                CType(pcklistproductHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(179, 179, 179)
            ElseIf result1 <= 0 And result2 >= 0 Then
                'MsgBox("present")
                CType(pcklistproductHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(171, 217, 93)
            ElseIf result1 > 0 And result2 > 0 Then
                'MsgBox("future")
                CType(pcklistproductHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(122, 156, 255)
            End If
        Next
    End Sub


    Private Sub addMaspriceByPckg_Click(sender As Object, e As EventArgs) Handles addMaspriceByPckg.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If PCKPIDTxt.Text.ToString.Length > 0 And OPDPriceFormulaTxt.Text.ToString.Length > 0 And IPDPriceFormulaTxt.Text.ToString.Length > 0 And pckSTDate.Text.ToString.Length > 0 Then
                    Dim objInsertData As New masPrice
                    If PRCID2Txt.Text.ToString.Length > 0 Then
                        objInsertData.PRCID_ = PRCID2Txt.Text
                    End If

                    objInsertData.PCKPID_ = PCKPIDTxt.Text

                    Dim startDay As String = pckSTDate.Value.Day
                    Dim startMonth As String = pckSTDate.Value.Month
                    Dim startYear As String = pckSTDate.Value.Year
                    objInsertData.ST_ = startYear & "-" & startMonth & "-" & startDay

                    objInsertData.OPD_PRICE_ = OPDPriceFormulaTxt.Text
                    objInsertData.IPD_PRICE_ = IPDPriceFormulaTxt.Text
                    objInsertData.STATUS_ = maspriceStatusChk.Checked

                    objInsertData.insertMaspricePackage()

                    Dim objSelectMasproductWithPriceDate As New masPrice
                    Dim maspriceDataByPackage As DataTable
                    maspriceDataByPackage = connect.GetTable(objSelectMasproductWithPriceDate.selectPckproduct_)
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    pckproductGrid.PrimaryGrid.DataSource = maspriceDataByPackage
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class